-
Notifications
You must be signed in to change notification settings - Fork 74
tagged defaults #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
tagged defaults #45
Conversation
Ah it looks like IsExported() needs go >= 1.17. I could just check the case of the first char of the field? |
See golang/go#41563, basically you'll want to check if |
@@ -34,6 +34,16 @@ func Set(ptr interface{}) error { | |||
|
|||
for i := 0; i < t.NumField(); i++ { | |||
if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" { | |||
if t.Field(i).IsExported() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if t.Field(i).IsExported() {
// Check whether the field is exported
if t.Field(i).PkgPath == "" {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@servusdei2018 your suggestion appears to be incorrect:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, thanks for catching that! fixed it
I would utilize https://pkg.go.dev/encoding/json#Unmarshaler or https://pkg.go.dev/encoding#TextUnmarshaler interfaces instead. |
This PR is breaking this promise: |
Hey there! I'd love to be able to set a custom default for a type based on the tag like so:
This PR adds the interface and modifies the Set() logic to use it. Added tests to account for success and failure. All tests passing. Thanks!